﻿<template>
  <a-drawer
    :title="title"
    width="90%"
    :visible="visible"
    @close="
      () => {
        this.visible = false
      }
    "
  >
    <a-table
      ref="table"
      :scroll="{ y: tableHight }"
      :columns="columns"
      :rowKey="(row, index) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'checkbox' }"
      :bordered="true"
      size="small"
      :customRow="loadRow">
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime,'YYYY-MM-DD HH:mm') }}</span>

      <template slot="TotalNum" slot-scope="text">
        <span>{{ text.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) }}</span>
      </template>
      <template slot="TotalAmount" slot-scope="text">
        <span>{{ text.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) }}</span>
      </template>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if=" record.State!==0" @click.stop="handleView(record.Id)">查看</a>
        </template>
      </span>
    </a-table>
    <edit-form ref="editForm" :parentObj="{...this}" @returnMain="handleReturnMain"></edit-form>
  </a-drawer>
</template>

<script>
import EditForm from './EditForm'
const columns = [
  { title: '销售订单号', dataIndex: 'cSOCode', width: '10%' },
  {
    title: '单据日期',
    dataIndex: 'dDate',
    align: 'center',
    customRender: (value, row) => {
      return row.dDate.substring(0, 10)
    },
    width: '10%'
  },
  { title: '客户编码', dataIndex: 'cCusCode', width: '10%' },
  { title: '客户名称', dataIndex: 'cCusName', width: '20%' },
  { title: '销售类型', dataIndex: 'cSTName', align: 'center', width: '8%' },
  { title: '总数量', dataIndex: 'TotalNum', align: 'center', width: '10%', scopedSlots: { customRender: 'TotalNum' } },
  { title: '总金额', dataIndex: 'TotalAmount', align: 'center', width: '10%', scopedSlots: { customRender: 'TotalAmount' } },
  {
    title: '修改日期',
    dataIndex: 'dmoddate',
    align: 'center',
    customRender: (value, row) => {
      return row.dmoddate === null ? '' : row.dmoddate.substring(0, 19)
    },
    width: '10%'
  },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' }, width: '140px' }
]

export default {
  components: {
    EditForm
  },
  mounted () {
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHight = this.getTableHeight(true) + 24.5
    })
    this.getDataList()
  },
  data () {
    return {
      tableHight: 570, // 表格高度
      data: [],
      title: '',
      visible: false,
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['20', '30', '40', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRows: [],
      selectedRowKeys: []
    }
  },
  methods: {
    reset () {
      this.queryParam = {}
      this.pagination.current = 1
      this.pagination.pageSize = 20
      this.getDataList()
    },
    openForm (id, title) {
      this.title = title
      this.visible = true
      this.queryParam = { condition: 'SoMainId', keyword: id }
      this.pagination.current = 1
      this.pagination.pageSize = 20
      this.getDataList()
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/OrderManagement/So_SoMain_History/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order || 'desc',
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    inputKeyDown (e) {
      if (e.keyCode === 13) {
        if (!this.queryParam[0].keyword) return this.$message.error('请输入查询条件')
        this.getDataList()
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleView (id) {
      this.$refs.editForm.openForm(id, '编辑销售订单主表信息', true)
    },
    handleReturnMain () {
      this.getDataList()
    },
    // 表格点击选中取消
    loadRow (record, index) {
      return {
        on: {
          click: () => {
            if (record.disabled) return
            const key = record.Id
            if (this.selectedRowKeys.includes(key)) {
              const index = this.selectedRowKeys.findIndex(skey => skey === key)
              this.selectedRowKeys.splice(index, 1)
              this.selectedRows.splice(index, 1)
            } else {
              this.selectedRowKeys.push(key)
              this.selectedRows.push(record)
            }
          }
        }
      }
    },
    // 更新选中操作
    updateSelect (selectedRowKeys, selectedRows) {
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    }
  }
}
</script>
